package com.mdlib.helper.ui.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ljlib.core.util.EmptyUtils;
import com.ljlib.help.R;
import com.mdlib.helper.event.VoucherEvent;
import com.mdlib.helper.model.entity.VoucherEntity;
import com.mdlib.helper.ui.adapter.VoucherDialogAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：HQY on 16/11/21 15:10
 * 邮箱：hqy_xz@126.com
 */

public class VoucherDialogFragment extends DialogFragment {


    private static final String VOUCHER = "voucher";
    @Bind(R.id.rv_voucher_lsit)
    RecyclerView mRvVoucherLsit;
    @Bind(R.id.tv_voucher_submit)
    TextView mTvVoucherSubmit;
    @Bind(R.id.iv_service_money_bg)
    ImageView mIvServiceMoneyBg;
    @Bind(R.id.rl_minnetonka_service_money)
    RelativeLayout mRlMinnetonkaServiceMoney;
    @Bind(R.id.ll_voucher_close)
    LinearLayout mLlVoucherClose;

    private VoucherEntity voucherInfo;

    private List<VoucherEntity> voucherList = new ArrayList<>();

    private VoucherDialogAdapter mVoucherDialogAdapter;

    public static VoucherDialogFragment newInstance(ArrayList<VoucherEntity> vouchers) {
        VoucherDialogFragment fragment = new VoucherDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(VOUCHER, vouchers);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (EmptyUtils.isEmpty(getArguments())) {
            dismiss();
        }
        voucherList = (List<VoucherEntity>) getArguments().getSerializable(VOUCHER);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.Dialog_Share);
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_voucher, null);
        builder.setView(view);
        ButterKnife.bind(this, view);
        init();
        return builder.create();
    }


    @Override
    public void onStart() {
        super.onStart();

        // 设置宽度为屏宽、靠近屏幕底部。
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        window.setWindowAnimations(R.style.bottomShow);
        wlp.gravity = Gravity.BOTTOM;
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(wlp);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    public void init() {

        mVoucherDialogAdapter = new VoucherDialogAdapter(voucherList);
        mRvVoucherLsit.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvVoucherLsit.setAdapter(mVoucherDialogAdapter);
        mRvVoucherLsit.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

            }

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (voucherInfo != null) {
                    voucherInfo.setSelected(false);
                }

                VoucherEntity voucherEntity = voucherList.get(position);

                if (voucherEntity == voucherInfo) {
                    if (voucherEntity.isSelected()) {
                        voucherEntity.setSelected(true);
                    }
                }
                voucherEntity.setSelected(true);
                voucherInfo = voucherEntity;
                mVoucherDialogAdapter.notifyDataSetChanged();

            }
        });


    }


    @OnClick({R.id.tv_voucher_submit, R.id.ll_voucher_close})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_voucher_submit:
                EventBus.getDefault().post(new VoucherEvent(voucherInfo));
                dismiss();
                break;
            case R.id.ll_voucher_close:
                dismiss();
                break;
        }
    }
}
